package com.sub.spark.sql.extend;

import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;

import static org.apache.spark.sql.functions.col;

/**
 * @ClassName SparkSqlDSL
 * @Description: Spark SQL DSL
 * @Author Submerge.
 * @Since 2025/5/24 13:40
 * @Version 1.0
 */
public class SparkSqlDSL {

    public static void main(String[] args) {

        SparkSession sparkSession = SparkSession.builder().appName("sub-spark-sql-dsl").master("local[2]").getOrCreate();

        Dataset<Row> json = sparkSession.read().json("data/demo/spark/user.json");

        json.createOrReplaceTempView("user");

        //SQL实现,session.sql()
        sparkSession.sql("select name,age from user where age > 20").show();

        //DSL实现,dataset.select()
        json.select(col("name").as("newName"),
                        col("age").plus(1).as("newAge"))
                .filter(col("age").gt(18));



        sparkSession.stop();

    }
}
